import * as vscode from "vscode";
import { CommandId } from "@ap/command";
export class ChatjptCodeActionProvider implements vscode.CodeActionProvider {
  provideCodeActions(
    document: vscode.TextDocument,
    range: vscode.Range | vscode.Selection,
    context: vscode.CodeActionContext,
    token: vscode.CancellationToken,
  ): vscode.ProviderResult<vscode.Command[] | vscode.CodeAction[]> {
    let actions: vscode.CodeAction[] = [];
    context.diagnostics.forEach((diagnostic) => {
      if (diagnostic.severity === vscode.DiagnosticSeverity.Error) {
        let text: string;
        const startLine = diagnostic.range.start.line;
        const endLine = diagnostic.range.end.line;
        if (startLine !== endLine) {
          text = document.getText(
            new vscode.Range(
              startLine,
              0,
              endLine,
              document.lineAt(endLine).range.end.character,
            ),
          );
        } else {
          text = document.lineAt(startLine).text;
        }
        let action = new vscode.CodeAction(
          "使用Chat吉PT修复 '" + document.getText(diagnostic.range) + "'",
          vscode.CodeActionKind.QuickFix,
        );
        action.command = {
          title: "使用Chat吉PT修复",
          command: CommandId.chatjpt_quickFix,
          arguments: [text, diagnostic.message],
        };
        actions.push(action);
      }
    });
    return actions;
  }
}
